﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using BLL;
using MOD;

namespace MapServer.Adhs
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Login : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            String sReturnValue = String.Empty;
            if (context.Request["userId"] != null && context.Request["password"] != null )
            {
                sReturnValue = LoginUserIdPassword(Convert.ToInt64(context.Request["userId"]), context.Request["password"].ToString().Trim());
            }
            if (context.Request["userName"] != null && context.Request["password"] != null)
            {
                sReturnValue = LoginUserNamePassword(context.Request["userName"].ToString().Trim(), context.Request["password"].ToString().Trim());
            }
            if (context.Request["weiboUserId"] != null && context.Request["password"] != null )
            {
                Int64 iWeiboFlag =0;
                if (context.Request["weiboFlag"] != null)
                {
                    iWeiboFlag = Convert.ToInt64(context.Request["weiboFlag"]);
                }
                sReturnValue = LoginWeiboUserId(Convert.ToInt64(context.Request["weiboUserId"]), context.Request["password"].ToString().Trim(),iWeiboFlag);
            }
            context.Response.Write(sReturnValue);
        }
        private String LoginUserIdPassword(Int64 iUserId, String sPassWord)
        {
            B_Users bUs = new B_Users();
            String sValue = "998";
            try
            {
                DataTable dDt = new DataTable();
                dDt = bUs.SelectUserIdPassWord(iUserId, sPassWord);
                if (dDt.Rows.Count != 0)
                {
                    if (dDt.Rows[0]["Password"].ToString().Trim() == sPassWord)
                    {
                        sValue = "001";
                    }
                    else
                    {
                        sValue = "003";
                    }
                }
                else
                {
                    sValue = "002";
                }
            }
            catch { }
            sValue = JsonParameters.CreateJsonParameters(String.Empty,sValue);
            return sValue;
        }
        private String LoginUserNamePassword(String sUserName, String sPassWord)
        {
            B_Users bUs = new B_Users();
            String sValue = "998";
            try
            {
                DataTable dDt = new DataTable();
                dDt = bUs.SelectUserNamePassWord(sUserName, sPassWord);
                if (dDt.Rows.Count != 0)
                {
                    if (dDt.Rows[0]["Password"].ToString().Trim() == sPassWord)
                    {
                        sValue = "001";
                    }
                    else
                    {
                        sValue = "003";
                    }
                }
                else
                {
                    sValue = "002";
                }
            }
            catch { }
            sValue = JsonParameters.CreateJsonParameters(String.Empty, sValue);
            return sValue;
        }
        private String LoginWeiboUserId(Int64 iWeiboUserId, String sPassWord,Int64 iWeiboFlag)
        {
            B_Users bUs = new B_Users();
            String sValue = "998";
            try
            {
                DataTable dDt = new DataTable();
                dDt = bUs.SelectUserWeiboId(iWeiboUserId, iWeiboFlag);
                if (dDt.Rows.Count != 0)
                {
                    if (dDt.Rows[0]["Password"].ToString().Trim() == sPassWord)
                    {
                        sValue = "001";
                    }
                    else
                    {
                        sValue = "003";
                    }
                }
                else
                {
                    sValue = "002";
                }
            }
            catch { }
            sValue = JsonParameters.CreateJsonParameters(String.Empty, sValue);
            return sValue;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
